home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / blankery / madhouse / developer / c-demo / demo.c < prev    next >
C/C++ Source or Header  |  1995-03-05  |  10KB  |  383 lines

  1. /*    demo.c
  2.     Shows how to use the Madhouse-interface with a C-like language.
  3.  
  4.    Instructions to compile the program:
  5.     - Perhaps your compiler doesn't like <pragma/...>, then use another
  6.       name for this directory.
  7.     - This program can be compiled without problems with MaxonC++ v3.
  8.       For other compilers, you might have to change something.
  9. */
  10.  
  11. #include <dos/dos.h>
  12. #include <intuition/intuition.h>
  13. #include <intuition/screens.h>
  14. #include <pragma/graphics_lib.h>
  15. #include <pragma/intuition_lib.h>
  16. #include <pragma/exec_lib.h>
  17. #include <pragma/dos_lib.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <time.h>
  21. #include <string.h>
  22.  
  23. #define ABNC 0x40
  24. #define ABC  0x80
  25.  
  26.  
  27. /* Prototypes */
  28. void write_stopblank();
  29. void read_prefs();
  30. void write_error( char *str );
  31.  
  32.  
  33. /* Variables */
  34. char text[40];
  35. short color, scroll;
  36. time_t start;
  37. short duration_sec=0;
  38. struct Library *IntuitionBase = NULL;
  39. struct Library *GfxBase = NULL;
  40. struct Screen *scr = NULL;
  41. struct Window *win = NULL;
  42. struct Message *msg = NULL;
  43. USHORT DemoPalette[] = {
  44.     0x0000, 0x0FFF
  45. };
  46. struct TextAttr topaz8 = {
  47.     "topaz.font",
  48.     8,
  49.     0,
  50.     0
  51. };
  52.  
  53. /***************************************************************************
  54. ***                          open_all()
  55. ***                  Opens all things we need.
  56. ***************************************************************************/
  57.  
  58. BOOL open_all()
  59. {
  60.     IntuitionBase = OpenLibrary("intuition.library",37);
  61.     if( !IntuitionBase ) return FALSE;
  62.     GfxBase = OpenLibrary("graphics.library",37);
  63.     if( !GfxBase ) return FALSE;
  64.  
  65.     /* Open a standard LowRes-screen. */
  66.     scr = OpenScreenTags(NULL,
  67.           SA_Depth,         1,
  68.           SA_Width,         320,
  69.         SA_Height,        256,
  70.           SA_Font,          &topaz8,
  71.           TAG_DONE);
  72.     if(!scr) return FALSE;
  73.     
  74.     /* Set the colors of the mouse-pointer to the background-colors. */
  75.     SetRGB4( &scr->ViewPort,17,0,0,0);
  76.     SetRGB4( &scr->ViewPort,18,0,0,0);
  77.     SetRGB4( &scr->ViewPort,19,0,0,0);
  78.     
  79.     win = OpenWindowTags(NULL,
  80.              WA_AutoAdjust,    TRUE,
  81.              WA_NoCareRefresh, TRUE,
  82.              WA_CustomScreen,  scr,
  83.              WA_Flags,         WFLG_RMBTRAP,
  84.              WA_Borderless,    TRUE,
  85.              WA_Activate,      TRUE,
  86.              WA_IDCMP,             IDCMP_MOUSEBUTTONS+IDCMP_RAWKEY,
  87.              TAG_DONE );
  88.     if( !win ) return FALSE;
  89.     
  90.     LoadRGB4( &scr->ViewPort, DemoPalette, 2 );
  91.     
  92.     return TRUE;
  93. }
  94.  
  95. /***************************************************************************
  96. ***                          close_all()
  97. ***                 Closes all things we needed.
  98. ***************************************************************************/
  99.  
  100. void close_all()
  101. {
  102.     /* This brings the screen behind all others and should avoid some
  103.        stupid effects when closing screen with 68000-Amigas. */
  104.     if( scr )
  105.         ScreenToBack( scr );
  106.     
  107.     /* If something went wrong, we should tell this Madhouse. But mustn't
  108.        write more then one lines in the errors-file. So we use this nice
  109.         if-construct: */
  110.     if( !IntuitionBase ) write_error( "No intuition.lib V37!" );
  111.     else {
  112.         if( !GfxBase ) write_error( "No graphics.lib V37!" );
  113.         else {
  114.             if( !scr ) write_error( "Out of memory: Couldn't open screen.");
  115.             else {
  116.                 if( !win ) write_error( "Out of memory: Couldn't open window.");
  117.             }
  118.         }
  119.     }
  120.     
  121.     /* Now we close all what we could open. */
  122.     if( win ) CloseWindow( win );
  123.     if( scr ) CloseScreen( scr );
  124.     if( GfxBase ) CloseLibrary( GfxBase );
  125.     if( IntuitionBase ) CloseLibrary( IntuitionBase );
  126. }
  127.  
  128. /***************************************************************************
  129. ***                          main()
  130. ***                Here the real blanker begins.
  131. ***************************************************************************/
  132.  
  133. void main()
  134. {
  135.     BOOL quit = FALSE;
  136.     BOOL user_quit = FALSE;
  137.     short ypos = 20;
  138.     short direction = 1;
  139.     short oldy;
  140.     
  141.     /* First read the settings for our blanker and the duration-value.*/
  142.     read_prefs();
  143.     
  144.     /* Now we start our stop-watch. */
  145.     start = time(NULL);
  146.     
  147.     /* Open screen and window. */
  148.     if(open_all()) {
  149.         
  150.         /* Color-parameter: set the right color. */
  151.         switch( color ) {
  152.             case 0:
  153.                 SetRGB4( &scr->ViewPort, 1, 15,0,0 );
  154.                 break;
  155.             case 1:
  156.                 SetRGB4( &scr->ViewPort, 1, 15,15,0 );
  157.                 break;
  158.             case 2:
  159.                 SetRGB4( &scr->ViewPort, 1, 0,0,15 );
  160.         }
  161.         
  162.         /* Text-parameter: write the text. */
  163.         SetAPen( win->RPort, 1 );
  164.         Move( win->RPort, 5, ypos );
  165.         Text( win->RPort, text, ( strlen(text) > 38 ? 38 : strlen(text)) );
  166.         
  167.         /* Main-Loop. */
  168.         while( !quit ) {
  169.             
  170.             /* If the user selected Scroll, scroll the text. */
  171.             if( scroll ) {
  172.                 oldy = ypos;
  173.                 ypos += direction;
  174.                 if( (ypos > 220)  ||  (ypos < 20) ) direction *= -1;
  175.                 
  176.                 ClipBlit(win->RPort, 5, oldy-10, win->RPort, 5, ypos-10, 310, 20, ABNC | ABC );
  177.                 /* Minterms ABNC | ABC -> copy the rectangle without modifications. */
  178.             }
  179.             
  180.             /* Mousbutton or key? */
  181.             if( msg = GetMsg(win->UserPort) ) {
  182.                 quit = TRUE;
  183.                 user_quit = TRUE;
  184.                 ReplyMsg( msg );
  185.             }
  186.             
  187.             /* When 'Change Blanker' is activated ( duration_sec is not 0 )
  188.                AND we blanked (one second) longer as we are allowed to
  189.                 ( difftime(time(NULL),start) > time_counter ) then quit. */
  190.             if( duration_sec && difftime(time(NULL),start) > duration_sec )
  191.                 quit = TRUE;
  192.             
  193.             
  194.             WaitTOF();
  195.         }
  196.         
  197.     }
  198.     
  199.     /* Close our stuff and -perhaps- write the error. */
  200.     close_all();
  201.     
  202.     /* If the user stopped our Blanker, we have to inform Madhouse about
  203.        this, otherwise it would perhaps (if 'Change Blanker' is activated)
  204.        start a new blanker. */
  205.     
  206.     if( user_quit )
  207.         write_stopblank();
  208.     
  209.     /* And finished! */
  210. }
  211.  
  212.  
  213. /***************************************************************************
  214. ***                                                                      ***
  215. ***              Functions for the communication with Madhouse           ***
  216. ***                                                                      ***
  217. ***************************************************************************/
  218.  
  219. /* Some global variables: */
  220.  
  221. char bu[1500];        /* The buffer bu[] includes the whole prefs-file. */
  222. short bu_seek = 0;    /* bu_seek is the read-offset of bu[]. We need this
  223.                          variable to see where we have to read the next
  224.                          parameter. */
  225.  
  226. /* First some useful functions: */
  227.  
  228. /***************************************************************************
  229. ***                          read_next_digit()
  230. ***              Reads the next parameter out of our prefs-file.
  231. ***                   The next parameter must be a number.
  232. ***************************************************************************/
  233.  
  234. long read_next_digit()
  235. {
  236.     short number_cnt = 0;
  237.     char number[50];
  238.     
  239.     while( bu[bu_seek] != (char) 0x0D ) {
  240.         number[number_cnt] = bu[bu_seek];
  241.         bu_seek++;
  242.         number_cnt++;
  243.     }
  244.     number[number_cnt] = (char) 0x00;
  245.  
  246.     /*  Overjump the (0x0D)(0x0A)
  247.      *  (Madhouse places these sequences between the lines.)   */
  248.     bu_seek+=2;
  249.     
  250.     return strtol( number, 0, 10 );
  251. }
  252.  
  253. /***************************************************************************
  254. ***                          read_next_string()
  255. ***              Reads the next parameter out of our prefs-file.
  256. ***                   The next parameter must be a string.
  257. ***************************************************************************/
  258.  
  259. void read_next_string( char *string, short maxlen )
  260. {
  261.     short text_cnt = 0;
  262.     
  263.     /* A Madhouse-Text begins begins with a "$", so we jump over the first
  264.        char. */
  265.     bu_seek++;
  266.     
  267.     
  268.     while( (bu[bu_seek] != (char) 0x0D)  &&  (text_cnt < maxlen)  ) {
  269.         string[text_cnt] = bu[bu_seek];
  270.         bu_seek++;
  271.         text_cnt++;
  272.     }
  273.     
  274.     string[text_cnt] = (char) 0x00;
  275.     /* Now we have the text in string[]. */
  276.     
  277.     
  278.     /*
  279.      *  Jump over the (0x0D)(0x0A)
  280.      */
  281.     
  282.     bu_seek+=2;
  283. }
  284.  
  285.  
  286.  
  287.  
  288. /***************************************************************************
  289. ***                             read_prefs()
  290. ***                        Reads the preferences.
  291. ***************************************************************************/
  292.  
  293. void read_prefs()
  294. {
  295.     short text_cnt = 0;
  296.     short number_cnt = 0;
  297.     BPTR f = NULL;
  298.     
  299.     f = Open( "RAM:Madhouse_Storage/prefs", MODE_OLDFILE );
  300.     if (f) {
  301.         Read( f, bu, sizeof( bu ) );
  302.         
  303.         /* Now, bu[] has a contents like this:
  304.            "$Hello, this is a text(0x0D)(0x0A)2(0x0D)(0x0A)0(0x0D)(0x0A)
  305.             5(0x0D)(0x0A)RAM:Madhouse_Storage(0x0D)(0x0A)".
  306.            (0x0D) and (0x0A) are between every line.
  307.            Hello,... is our first parameter, 2 our second, 0 our third,
  308.             5 the duration in minutes and RAM:.. a path to our directory,
  309.             if we need data from it.
  310.             All Madhouse-strings begin with a "$".
  311.            We will put the first parameter in text[], the second in color,
  312.             the duration in duration_sec and the path is not needed. */
  313.         
  314.         /*
  315.          *   First parameter (Text)
  316.          */
  317.          
  318.         read_next_string( text, 40 );
  319.         
  320.         /*
  321.          *  Second parameter (Color)
  322.          */
  323.         
  324.         color = read_next_digit();
  325.         
  326.         /*
  327.          *  Third parameter (Scroll)
  328.          */
  329.         
  330.         scroll = read_next_digit();
  331.         
  332.         /*
  333.          *  Duration-Parameter.
  334.          */
  335.         
  336.         duration_sec = read_next_digit() *  60; /*Minutes -> Seconds*/
  337.         
  338.         Close( f );
  339.     } else {
  340.         /* The Blanker was not started by Madhouse. Quit here.*/
  341.         exit(0);
  342.     }
  343. }        
  344.  
  345. /***************************************************************************
  346. ***                           write_stopblank()
  347. ***           Must be used if the user aborted the blanker himself.
  348. ***************************************************************************/
  349.  
  350. void write_stopblank()
  351. {
  352.     /* Just create a new empty file to inform Madhouse that the user has
  353.        aborted the blanker. */
  354.     
  355.     BPTR f;
  356.     f = Open( "RAM:Madhouse_Storage/stopblank", MODE_NEWFILE );
  357.     if( f )
  358.         Close( f );
  359. }
  360.  
  361. /***************************************************************************
  362. ***                           write_error()
  363. ***      Tells Madhouse that an error occurred - can only be used ONCE!
  364. ***************************************************************************/
  365.  
  366. void write_error( char *str )
  367. {
  368.     BPTR f;
  369.     char eol_marker = 0x0D;
  370.     
  371.     f = Open( "RAM:Madhouse_Storage/errors", MODE_READWRITE );
  372.     if( f ) {
  373.         Seek( f, 0, OFFSET_END );
  374.         Write( f, str, strlen(str) );
  375.         Write( f, &eol_marker, 1 );
  376.         Close( f );
  377.     } 
  378. }
  379.  
  380.  
  381.  
  382.  
  383.